package com.by.ui;

import android.animation.Animator;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.widget.ImageView;

/**
 * 模仿微信查看大图
 * 需要优化<1>图片开始以Center_Crop方式显示，先变化至图片原来大小，切换显示方式FitCenter,动画过度至整个屏幕大小
 */
public class BigPicActivity extends Activity {
    View layout;
    private ImageView img;
    int mleft;
    int mtop;
    float sx;
    float sy;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_big_pic);
        layout = findViewById(R.id.layout);
        img = (ImageView) findViewById(R.id.img);
        img.setScaleType(ImageView.ScaleType.FIT_XY);
        img.setImageResource(R.drawable.dd);

        img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.e("onClick", "mleft:" + mleft + ",mtop:" + mtop + ",sx:" + sx + ",sy:" + sy);
                img.animate()
                        .scaleX(sx).scaleY(sy)
                        .translationX(mleft).translationY(mtop)
                        .setInterpolator(new DecelerateInterpolator())
                        .setListener(new Animator.AnimatorListener() {
                            @Override
                            public void onAnimationStart(Animator animator) {
                            }

                            @Override
                            public void onAnimationEnd(Animator animator) {
                                layout.setVisibility(View.GONE);
                                finish();
                            }

                            @Override
                            public void onAnimationCancel(Animator animator) {

                            }

                            @Override
                            public void onAnimationRepeat(Animator animator) {

                            }
                        })
                        .setDuration(500L).start();
            }
        });
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {
            Intent intent = getIntent();
            int left = intent.getIntExtra("left", 0);
            int top = intent.getIntExtra("top", 0);
            int width = intent.getIntExtra("width", 0);
            int height = intent.getIntExtra("height", 0);
            int[] location = new int[2];
            img.getLocationOnScreen(location);
            img.setPivotX(0);
            img.setPivotY(0);
            mleft = left - location[0];
            img.setTranslationX(mleft);
            mtop = top - location[1];
            img.setTranslationY(mtop);
            sx = 1.0f * width / img.getWidth();
            sy = 1.0f * height / img.getHeight();
            Log.e("img", "img:width:" + img.getWidth() + ",height:" + img.getHeight());
            img.setScaleX(sx);
            img.setScaleY(sy);
            Log.e("TAG", "mleft:" + mleft + ",mtop:" + mtop + ",sx:" + sx + ",sy:" + sy);


            img.animate()
                    .scaleX(1).scaleY(1)
                    .translationX(0).translationY(0)
                    .setInterpolator(new AccelerateInterpolator())
                    .setDuration(500L).start();

        }
    }

    @Override
    public void onBackPressed() {
        finish();
    }

    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);
        overridePendingTransition(0, 0);
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(0, 0);
    }
}
